<template>
  <div class="factory-procurement-page">
    <div class="content-wrapper">
      <!-- 页面头部 -->
      <div class="page-header">
        <div class="header-content">
          <div class="header-title">
            <h1>工厂直收专区</h1>
            <p>直连各大收购工厂，提供实时收购价格，助力菇农实现优质优价</p>
          </div>
          <!-- <div class="search-bar">
          <el-input
            v-model="searchForm.keyword"
            placeholder="搜索工厂名称、地区或产品..."
            clearable
            @keyup.enter="handleSearch"
            class="search-input"
          />
          <div class="search-info">
            <span>当前展示{{ total }}个工厂</span>
            <el-button type="danger" @click="handleSearch" class="search-btn">搜索</el-button>
          </div>
        </div> -->
        </div>
      </div>

      <!-- 工厂收购列表 -->
      <div class="list-section">
        <!-- 加载状态 -->
        <div v-if="loading" class="loading-container">
          <el-skeleton :rows="5" animated />
        </div>

        <!-- 工厂列表 -->
        <div v-else-if="procurementList.length > 0" class="procurement-companies">
          <div v-for="company in procurementList" :key="company.id" class="company-card">
            <!-- 企业头部信息 -->
            <div class="company-header">
              <div class="company-name">{{ company.factoryName }}</div>
              <div class="certification-badge">
                <span class="badge-icon">✓</span>
                平台认证企业
              </div>
              <div class="update-time">
                <span class="time-icon">i</span>
                更新时间：{{ company.updateTime || '暂无' }}
              </div>
            </div>

            <!-- 联系信息 -->
            <div class="company-contact">
              <div class="contact-item">
                <span class="contact-icon">📍</span>
                <span>{{
                  company.province + company.city + company.district + company.addressDetail || '地址待完善'
                }}</span>
              </div>
              <div class="contact-item">
                <span class="contact-icon">📞</span>
                <span> {{ company.landlinePhone || '' }} &nbsp; &nbsp;{{ company.mobilePhone || '电话待完善' }}</span>
              </div>
              <div class="contact-item">
                <span class="contact-icon">🕒</span>
                <span>营业时间：{{ company.businessHours }}</span>
                &nbsp;&nbsp; &nbsp;&nbsp;
                <span class="time-icon">⏳</span>
                报价有效期：{{ company.quoteValidPeriod }}
              </div>
            </div>

            <!-- 特别说明 -->
            <div class="company-note">
              <span class="note-icon">📋</span>
              <span>{{ company.description || '价格根据市场行情每日更新，以当日到厂实际检测品质为准' }}</span>
            </div>

            <!-- 收购价格表 -->
            <div class="price-table">
              <table>
                <thead>
                  <tr>
                    <th>规格等级</th>
                    <th>尺寸</th>
                    <th>收购价格(元/斤)</th>
                    <th>品质要求</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="item in company.procurementItems" :key="item.id">
                    <td>{{ item.gradeName }}</td>
                    <td>{{ item.sizeSpec }}</td>
                    <td class="price-cell">{{ item.purchasePrice }}</td>
                    <td>{{ item.qualityRequirements }}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>

        <!-- 空状态 -->
        <div v-else class="empty-state">
          <el-empty description="暂无工厂收购数据" />
        </div>

        <!-- 分页 -->
        <div v-if="total > 0" class="pagination">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 50, 100]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { useFactoryProcurement } from '~/composables/useFactoryProcurement'
import { ElMessage } from 'element-plus'

// 使用工厂收购组合式函数
const {
  loading,
  procurementList,
  total,
  currentPage,
  pageSize,
  fetchProcurementList,
  handlePageChange,
} = useFactoryProcurement()
console.log(procurementList, 'procurementList')
// 搜索表单
const searchForm = reactive({
  keyword: '',
})

// 搜索处理
const handleSearch = async () => {
  currentPage.value = 1
  await loadProcurementList()
}

// 加载工厂收购列表
const loadProcurementList = async () => {
  try {
    await fetchProcurementList({
      ...searchForm,
      page: currentPage.value,
      limit: pageSize.value,
    })
  } catch (error) {
    // ElMessage.error('加载工厂收购列表失败')
  }
}

// 分页处理
const handleSizeChange = (val: number) => {
  pageSize.value = val
  currentPage.value = 1
  loadProcurementList()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  loadProcurementList()
}

// 页面加载时获取数据
onMounted(() => {
  loadProcurementList()
})
</script>

<style scoped lang="scss">
.factory-procurement-page {
  background-color: #ffffff;
  padding: 40px 0;
  margin: 20px 0;
  width: 100vw;
  margin-left: calc(50% - 50vw);
  margin-right: calc(50% - 50vw);
  min-height: 100vh;

  .content-wrapper {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;
  }
}

.page-header {
  background: #fff;
  padding: 20px 0;
  margin-bottom: 15px;
  border-bottom: 1px solid #e4e7ed;

  .header-content {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;
  }

  .header-title {
    text-align: center;
    margin-bottom: 15px;

    h1 {
      margin: 0 0 8px 0;
      color: #333;
      font-size: 36px;
      font-weight: 700;
      letter-spacing: 1px;
    }

    p {
      margin: 0;
      color: #666;
      font-size: 16px;
      line-height: 1.6;
    }
  }

  .search-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    max-width: 800px;
    margin: 0 auto;

    .search-input {
      flex: 1;
      max-width: 500px;

      :deep(.el-input__wrapper) {
        border-radius: 25px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        border: 1px solid #e4e7ed;
        padding: 12px 20px;
        font-size: 14px;

        &:hover {
          border-color: #d9d9d9;
        }

        &.is-focus {
          border-color: #d9d9d9;
          box-shadow: 0 0 0 2px rgba(217, 217, 217, 0.2);
        }
      }
    }

    .search-info {
      display: flex;
      align-items: center;
      gap: 15px;

      span {
        color: #666;
        font-size: 14px;
        white-space: nowrap;
      }

      .search-btn {
        border-radius: 25px;
        padding: 12px 30px;
        font-weight: 500;
        font-size: 14px;
        box-shadow: 0 2px 8px rgba(220, 38, 38, 0.3);
        border: none;

        &:hover {
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(220, 38, 38, 0.4);
        }
      }
    }
  }
}

.list-section {
  .loading-container {
    padding: 40px;
  }

  .procurement-companies {
    display: flex;
    flex-direction: column;
    gap: 20px;
  }

  .company-card {
    background: white;
    border: 1px solid #e4e7ed;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.3s ease;
    padding: 30px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      transform: translateY(-2px);
    }

    .company-header {
      display: grid;
      grid-template-columns: 1fr auto 1fr;
      align-items: center;
      margin-bottom: 20px;
      gap: 15px;

      .company-name {
        font-size: 20px;
        font-weight: 600;
        color: #333;
      }

      .certification-badge {
        display: flex;
        align-items: center;
        background: linear-gradient(45deg, rgb(72, 187, 120) 0%, rgb(47, 133, 90) 100%);
        color: white;
        padding: 6px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 500;
        justify-self: center;

        .badge-icon {
          margin-right: 4px;
          font-size: 10px;
        }
      }

      .update-time {
        display: flex;
        align-items: center;
        color: #1890ff;
        font-size: 14px;
        justify-self: end;

        .time-icon {
          width: 16px;
          height: 16px;
          background: #1890ff;
          color: white;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 10px;
          font-weight: bold;
          margin-right: 6px;
        }
      }

      .valid-period {
        display: flex;
        align-items: center;
        color: #1890ff;
        font-size: 14px;
        justify-self: end;

        .time-icon {
          width: 16px;
          height: 16px;
          background: #1890ff;
          color: white;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 10px;
          font-weight: bold;
          margin-right: 6px;
        }
      }
    }

    .company-contact {
      margin-bottom: 20px;

      .contact-item {
        display: flex;
        align-items: center;
        margin-bottom: 8px;
        font-size: 14px;
        color: #666;

        .contact-icon {
          margin-right: 8px;
          font-size: 16px;
        }
      }
    }

    .company-note {
      display: flex;
      align-items: flex-start;
      margin-bottom: 25px;
      padding: 15px;
      background: #f0f8ff;
      border-radius: 8px;
      border-left: 4px solid #1890ff;

      .note-icon {
        margin-right: 8px;
        font-size: 16px;
        color: #1890ff;
        margin-top: 2px;
      }

      span:last-child {
        font-size: 14px;
        color: #333;
        line-height: 1.5;
      }
    }

    .price-table {
      overflow-x: auto;

      table {
        width: 100%;
        border-collapse: collapse;
        background: white;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

        thead {
          background: #f5f5f5;

          th {
            padding: 15px 12px;
            text-align: left;
            font-weight: 600;
            color: #333;
            font-size: 14px;
            border-bottom: 2px solid #e8e8e8;
          }
        }

        tbody {
          tr {
            transition: background-color 0.2s ease;

            &:hover {
              background: #f9f9f9;
            }

            &:not(:last-child) {
              border-bottom: 1px solid #f0f0f0;
            }
          }

          td {
            padding: 15px 12px;
            font-size: 14px;
            color: #333;
            vertical-align: top;

            &.price-cell {
              font-weight: 600;
              color: #ff4d4f;
              font-size: 16px;
            }
          }
        }
      }
    }
  }

  .empty-state {
    text-align: center;
    padding: 40px;
  }

  .pagination {
    margin-top: 20px;
    text-align: center;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .factory-procurement-page {
    padding: 10px;
  }

  .page-header {
    .header-title h1 {
      font-size: 28px;
    }

    .search-bar {
      flex-direction: column;
      gap: 15px;

      .search-input {
        max-width: 100%;
      }
    }
  }

  .company-card {
    padding: 20px;

    .company-header {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      gap: 10px;
    }

    .price-table table {
      font-size: 12px;

      th,
      td {
        padding: 10px 8px;
      }

      .price-cell {
        font-size: 14px;
      }
    }
  }
}
</style>
